<?php
/*
 * @Description    : 
 * @Version        : 1.0.0
 * @Author         : QianLong
 * @Date           : 2020-02-08 15:01:43
 * @LastEditors    : QianLong
 * @LastEditTime   : 2023-11-14 11:50:24
 */

declare(strict_types=1);

namespace App\Controller;

use App\Common\RedisCache;
use App\Constants\Status;
use App\Exception\ApiException;
use App\Service\Push\GoEasy;
use Qianlong\Validate\Validate;

class CommonController extends AbstractController
{
  public function getSocketInfo()
  {
    $valitor = new Validate();
    $valitor->addColumn('id')->required();
    if ($valitor && !$this->validate($valitor)) {
      throw new ApiException(Status::CODE_BAD_REQUEST, $valitor->getError()->__toString());
    }
    unset($valitor);
    $param = $this->request->all();
    $where['status'] = 1;
    $where['_id'] = $param['id'];
    $info = mongodb2()->push_server_list->findOne($where);
    $info = json_decode(json_encode($info), true);
    $return = [];
    if (!empty($info)) {
      $return['platform'] = '';
      $return['voice'] = $info['voice_url'];
      if ($info['type'] == 'app' || $info['type'] == 'inbox') {
        switch ($info['platform']) {
          case 'goeasy':
            $return['platform'] = $info['platform'];
            $return['key'] = $info['platform_goeasy_subscribe_key'];
            $return['channel'] = $info['platform_goeasy_channel'];
            $url = (new GoEasy($info))->getUrl();
            $return['host'] = str_replace('rest-','',parse_url($url)['host']);
            break;
          case 'ldp_socket':
            $socketServer = (new RedisCache)->getWsServer($info['platform_ldp_socket_id']);
            $return['platform'] = $info['platform'];
            $return['key'] = $info['platform_ldp_socket_id'];
            $return['key2'] = md5($socketServer['server_secret']);
            $return['channel'] = $info['platform_ldp_channel'];
            break;
          default:
            # code...
            break;
        }
      }else{
        if (in_array('goeasy', $info['platform_alert'])) {
          $return['platform'] = 'goeasy';
          $return['key'] = $info['platform_goeasy_subscribe_key'];
          $return['channel'] = $info['platform_goeasy_channel'];
          $url = (new GoEasy($info))->getUrl();
          $return['host'] = str_replace('rest-', '', parse_url($url)['host']);
        }
      }
    }

    // $return['info'] = $info;
    unset($userInfo, $where, $param, $skip, $time);
    return $this->response->json(Status::CODE_OK, 'success', $return);
  }
  public function home()
  {
    return $this->response->redirect('https://www.21ds.cn');
  }
}
